package com.hqu.environment.controller.app;

import com.hqu.environment.annotation.LoginUser;
import com.hqu.environment.dao.ElementStatusDao;
import com.hqu.environment.dao.FarmDao;
import com.hqu.environment.dao.UserDao;
import com.hqu.environment.dto.ElementStatusDTO;
import com.hqu.environment.dto.FarmDTO;
import com.hqu.environment.dto.UserDTO;
import com.hqu.environment.dto.UserFarmDTO;
import com.hqu.environment.utils.ExceptionCode;
import com.hqu.environment.utils.Result;
import com.hqu.environment.vo.TimeElementVO;
import com.hqu.environment.vo.TimeElementValueVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 农场Controller
 * @Date: 2019/04/25 22:15
 * @Author: 邓勇嘉
 **/
@Controller
@RequestMapping("app/farm")
public class FarmController {

    @Autowired
    FarmDao farmDao;

    @Autowired
    UserDao userDao;
    @Autowired
    ElementStatusDao elementStatusDao;


    @RequestMapping("/getFarmList")
    @ResponseBody
    public Result getFarmList(@LoginUser Integer userId) {
        List<FarmDTO> farmList = farmDao.selectListByUserId(userId);
        return Result.success().add("farmList",farmList);
    }

    @RequestMapping("/getSimpleFarmList")
    @ResponseBody
    public Result getSimpleFarmList(@LoginUser Integer userId) {
        List<FarmDTO> farmList = farmDao.getSimpleFarmList(userId);
        if(farmList.size()==0)
            return Result.fail(ExceptionCode.NO_OWN_FARM);
        return Result.success().add("farmList",farmList);
    }


    @RequestMapping("/getFarmById")
    @ResponseBody
    public Result getFarmById(Integer farmId) {
        FarmDTO farm = farmDao.selectByPrimaryKey(farmId);
        List<ElementStatusDTO> elementStatusList = elementStatusDao.selectListByFarmId(farmId);
        List<TimeElementVO> timeElement = elementStatusDao.selectTimeValue(farmId);
        timeElement=setElementValues(timeElement);

        return Result.success().add("farm",farm).add("elementStatusList",elementStatusList).add("timeElement",timeElement);
    }

    @RequestMapping("/getElementStatusById")
    @ResponseBody
    public Result getElementStatusById(Integer id) {
        ElementStatusDTO elementStatus = elementStatusDao.selectByPrimaryKey(id);
        return Result.success().add("elementStatus",elementStatus);
    }

    @RequestMapping("/saveElementStatus")
    @ResponseBody
    public Result saveElementStatus(ElementStatusDTO elementStatus) {
        elementStatusDao.updateByPrimaryKeySelective(elementStatus);


        return Result.success();
    }


    private List<TimeElementVO> setElementValues(List<TimeElementVO> timeElement){
        for(TimeElementVO item:timeElement){
            List<TimeElementValueVO> values= new ArrayList<>();
            int index=item.getValues().size()-8>=0?item.getValues().size()-8:0;
            for(int i =index;i<item.getValues().size();i++){
                values.add(item.getValues().get(i));
            }

            List<Double> valueTemp=new ArrayList<Double>();
            List<Date> timeTemp=new ArrayList<Date>();
            List<String> timeStrTemp=new ArrayList<String>();
            for(TimeElementValueVO temp:values){
                valueTemp.add(temp.getValue());
                timeTemp.add(temp.getTime());
                timeStrTemp.add(new SimpleDateFormat("HH点").format(temp.getTime()));
            }
            item.setValue(valueTemp);
            item.setTime(timeTemp);
            item.setTimeStr(timeStrTemp);
        }
        return timeElement;
    }


    @RequestMapping("/ownFarm")
    @ResponseBody
    public Result ownFarm(@LoginUser Integer userId,Integer farmId) {
        FarmDTO farmDTO = farmDao.selectByPrimaryKey(farmId);
        UserDTO userDTO = userDao.selectByPrimaryKey(userId);

        if(userDTO.getAuthority()<farmDTO.getAuthority()) return Result.fail(ExceptionCode.NO_AUTHORITY);

        UserFarmDTO uf = new UserFarmDTO();
        uf.setUser(userId);
        uf.setFarm(farmId);

        List<FarmDTO> farmList = farmDao.selectListByUserId(userId);
        for(FarmDTO f:farmList){
            if(f.getId()==farmId) return Result.fail(ExceptionCode.Farm_Exist);
        }
        farmDao.insertUserFarm(uf);
        return Result.success();
    }



    @RequestMapping("/deleteFarm")
    @ResponseBody
    public Result deleteFarm(@LoginUser Integer userId,Integer farmId) {

        UserFarmDTO uf = new UserFarmDTO();
        uf.setUser(userId);
        uf.setFarm(farmId);
        farmDao.deleteUserFarm(uf);
        return Result.success();
    }


    @RequestMapping("/checkFarmStatus")
    @ResponseBody
    public Result checkFarmStatus(@LoginUser Integer userId) {

        List<ElementStatusDTO> elementStatusList = elementStatusDao.selectListByUserId(userId);

        for(ElementStatusDTO item : elementStatusList){
            if(item.getValue()>item.getMonitorMax()||item.getValue()<item.getMonitorMin()){
                return Result.success().add("warning",true);
            }
        }
        return Result.success().add("warning",false);
    }
}
